package com.mavenq.fly.test.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

// 定义内部注解
@Retention(RetentionPolicy.RUNTIME)
@interface Permission {
    String value();
    String level() default "read";
}

// 定义包含注解数组的外部注解
@Retention(RetentionPolicy.RUNTIME)
@interface AccessControl {
    String description();
    Permission[] permissions(); // 注解数组
}

// 使用示例
public class AnnotationArrayDemo {

    @AccessControl(
        description = "管理员操作",
        permissions = {
            @Permission(value = "user_management", level = "write"),
            @Permission(value = "system_config", level = "admin")
        }
    )
    public void adminOperation() {
        System.out.println("执行管理员操作");
    }

    @AccessControl(
        description = "用户操作",
        permissions = {
            @Permission(value = "profile", level = "write"),
            @Permission(value = "documents", level = "read")
        }
    )
    public void userOperation() {
        System.out.println("执行用户操作");
    }

    private Object test(){
        return null;
    }

    // 处理注解的方法
    public static void processAnnotations() {
        Class<AnnotationArrayDemo> clazz = AnnotationArrayDemo.class;
        
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(AccessControl.class)) {
                AccessControl accessControl = method.getAnnotation(AccessControl.class);
                System.out.println("方法: " + method.getName());
                System.out.println("描述: " + accessControl.description());
                System.out.println("需要的权限:");
                
                for (Permission permission : accessControl.permissions()) {
                    System.out.println("  - " + permission.value() + " (" + permission.level() + ")");
                }
                System.out.println("---");
            }
        }
    }

    public static void main(String[] args) {
        // 演示处理注解
        processAnnotations();
        
        // 创建界面示例
//        javax.swing.JFrame frame = new javax.swing.JFrame("注解数组示例");
//        frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//        frame.setSize(400, 300);
//
//        javax.swing.JTextArea textArea = new javax.swing.JTextArea();
//        textArea.setText("查看控制台输出以了解注解数组的使用\n\n" +
//                        "注解中包含注解数组适用于:\n" +
//                        "1. 权限控制系统\n" +
//                        "2. API文档生成\n" +
//                        "3. 配置多个验证规则\n" +
//                        "4. 定义复杂的工作流程");
//        textArea.setEditable(false);
//        frame.add(new javax.swing.JScrollPane(textArea));
//
//        frame.setVisible(true);
    }
}